//
//  MainViewController.swift
//  weibo
//
//  Created by JiangLuo on 16/8/1.
//  Copyright © 2016年 JiangLuo. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setup();
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        //添加tabBar中的加号按钮
        tabBar.addSubview(composeBtn);
        
        let w = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: 49);
        
        composeBtn.frame = CGRectOffset(rect, 2*w , 0)
    }
    
    private lazy var composeBtn:UIButton = {
        let btn = UIButton();
        
        // 2.设置前景图片
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 3.设置背景图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action:"compseBtnClick" , forControlEvents: UIControlEvents.TouchUpInside);
        return btn;
    }();
    
    func compseBtnClick(){
        print(#function);
    }
    
    
    //从网络加载主菜单列表..
    private func getNetMenu(){
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings", ofType: "json");
        
        
        let jsonData = NSData(contentsOfFile: path!);
        
        do{
            let dicArray =  try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers);
            
            for dict in dicArray as! [[String: String]]
            {
                
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            };
            
        }catch{
            setup();
        }
    }
    
    
    private func setup(){
        // 从本地创建控制器
        addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
        addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
        
        // 再添加一个占位控制器
        addChildViewController("NullViewController", title: "", imageName: "")
        addChildViewController("DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
        addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        
    }

    private func addChildViewController(viewContorllerName: String,title:String,imageName:String) {
        
        //获取命名空间
        let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String;
        
        let vcClass = NSClassFromString(nameSpace+"."+viewContorllerName) as! UIViewController.Type;
        
        //初始化Class
        let vc = vcClass.init();
        
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: imageName);
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted");
        
        let nav = UINavigationController();
        nav.addChildViewController(vc);
        
        tabBar.tintColor = UIColor.orangeColor();
        addChildViewController(nav);
    }
   
    
    
    
}
